﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weight.Base;
using Weight.Model;
using Dapper;
using Dapper.Contrib.Extensions;
namespace Weight.Dal.CarManage
{
    public  class DalCarInfo
    {
        /// <summary>
        /// 查询车辆信息
        /// </summary>
        /// <param name="plateNum">车号</param>
        /// <returns></returns>
        public IEnumerable<CarInfoEntity> Query(string plateNum)
        {
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                string sql = "select * from CarInfo ";
                if(!string.IsNullOrEmpty(plateNum))
                {
                    plateNum = "%" + plateNum + "%";
                    sql += " where PlateNumber like @plateNum ";
                }
                sql += " order by CreateTime desc ";
               return conn.Query<CarInfoEntity>(sql,new { plateNum });
            }
        }

        /// <summary>
        /// 根据车号获取车辆信息是否存在
        /// </summary>
        /// <param name="plateNum">车号</param>
        /// <returns></returns>
        public CarInfoEntity GetByCarNo(string plateNum)
        {
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                string sql = "select * from CarInfo where PlateNumber=@plateNum";
                return conn.Query<CarInfoEntity>(sql, new { plateNum }).FirstOrDefault();
            }
        }

        /// <summary>
        /// 根据Id获取车辆信息是否存在
        /// </summary>
        /// <param name="plateNum">车号</param>
        /// <returns></returns>
        public CarInfoEntity GetById(string id)
        {
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                string sql = "select * from CarInfo where Id=@id";
                return conn.Query<CarInfoEntity>(sql, new { id }).FirstOrDefault();
            }
        }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Add(CarInfoEntity entity)
        {
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                conn.Insert(entity);
                return true;
            }
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(CarInfoEntity entity)
        {
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                return conn.Update(entity);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(CarInfoEntity entity)
        {
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                return conn.Delete(entity);
            }
        }
    }
}
